/*
* name;
*/
export default class ResMgr{
    /**
     * 加载预制体
     * @param url 
     * @param callback 
     */
    public static loadPrefab(url:string,callback:Function)
    {
        cc.loader.loadRes(url, function (err, prefab) {
            if (!err){
                if (callback != null){
                    callback(prefab);
                }
            }
        });
    }

    /**
     * 加载spine
     * @param url 
     * @param skt 
     * @param callback 
     */
    public static loadSpine(url:string, skt:sp.Skeleton, callback?:Function)
    {
        cc.loader.loadRes(url, sp.SkeletonData, function (err, spData) {
            if (!err){
                if (skt != null){
                    if (!cc.isValid(skt.node)) { return; }
                    skt.skeletonData = spData;
                }
                if (callback != null){
                    callback(spData);
                }
            }
        });
    }
    /**
     * 加载图集中的图片
     * @param url 
     * @param sub 
     * @param sp 
     * @param callback 
     */
    public static loadSpriteFrame(url:string,sub:string,sp:cc.Sprite,callback?:Function)
    {
        cc.loader.loadRes(url,cc.SpriteAtlas,function(err,atlas){
            if(err||!sp||!sp.isValid)return;
            if(atlas)
            {
                var sf = atlas.getSpriteFrame(sub);
                if(sf)
                {
                    sp.spriteFrame = sf;
                    if(callback)
                    {
                        callback(sp.spriteFrame);
                    }
                }
            }
        }.bind(this));
    }
    /**
     * 加载远程图片（微信头像）
     * @param url 
     * @param sp 
     * @param callback 
     */
    public static loadUrl(url:string,sp:cc.Sprite,callback?:Function)
    {
        cc.loader.load({url:url, type:'jpg'},function(err,data){
            if(err||!data||!sp||!sp.isValid)return;
            sp.spriteFrame = new cc.SpriteFrame(data);
            if(callback)
            {
                callback(sp.spriteFrame);
            }
        }.bind(this));
    }
    /**
     * 加载图片
     * @param url 
     * @param sp 
     * @param callback 
     */
    public static loadImage(url:string,sp:cc.Sprite,callback?:Function)
    {
        cc.loader.loadRes(url,cc.SpriteFrame,function(err,lsp){
            if(err||!lsp||!sp||!sp.isValid)return;
            sp.spriteFrame = lsp;
            if(callback)
            {
                callback(sp.spriteFrame);
            }
        }.bind(this));
    }

    /**
     * 加载Texture
     * @param url 
     * @param sp 
     * @param callback 
     */
    public static loadTexture(url:string,sp:any,callback?:Function)
    {
        cc.loader.loadRes(url,function(err,texture){
            if(err||!texture||!sp||!sp.isValid)return;
            sp.texture = texture;
            if(callback)
            {
                callback(sp.texture);
            }
        }.bind(this));
    }

    public static loadMaterial(url: string, callback?:Function) {
        cc.loader.loadRes(url, cc.Material, function (err, material) {
            if (!err){
                if (callback != null){
                    callback(err, material);
                }
            }
        });
    }

    public static loadRes(url: string, completeCallback: (error: Error, resource: any) => void)
    {
        cc.loader.loadRes(url,completeCallback);
    }

    public static loadTypeRes(url: string, type: typeof cc.Asset, completeCallback: (error: Error, resource: any) => void): void
    {
        cc.loader.loadRes(url,type,completeCallback);
    }

    public static loadResArray(url: string[], progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: ((error: Error, resource: any[]) => void)|null): void
    {
        cc.loader.loadResArray(url,progressCallback,completeCallback);
    }

    public static load(resources: string|string[]|{uuid?: string, url?: string, type?: string}, progressCallback: (completedCount: number, totalCount: number, item: any) => void, completeCallback: Function|null): void
    {
        cc.loader.load(resources,progressCallback,completeCallback);
    }

    public static getRes(url: string, type?: Function): any
    {
        return cc.loader.getRes(url,type);
    }

}